package com.fancy.learncenter.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.superservice.lya.R;


/**
 * Created by hyy on 2017/8/24.
 * describe as
 */

public class PopupWindowSendDiscuss extends PopupWindow {
    View rootView;

    Context mContext;

    View viewParent;

    public PopupWindowSendDiscuss(Context mContext, View viewParent) {
        this.mContext = mContext;
        this.viewParent = viewParent;
        initView();
    }

    LinearLayout discuss_root;
    TextView sendDiscuss;
    EditText discussStr;

    @SuppressLint("WrongConstant")
    private void initView() {
        rootView = LayoutInflater.from(mContext).inflate(R.layout.popupwindow_senddis, null);

        discuss_root = (LinearLayout) rootView.findViewById(R.id.discuss_root);

        sendDiscuss = rootView.findViewById(R.id.send);

        discussStr = (EditText) rootView.findViewById(R.id.ed_discuss);
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(null);

        this.update();


        this.setOutsideTouchable(true);

        this.setFocusable(true);

        setHeight(LinearLayout.LayoutParams.MATCH_PARENT);

        setWidth(LinearLayout.LayoutParams.MATCH_PARENT);

        rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        discuss_root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });

        sendDiscuss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sendDisCallBack != null) {
                    sendDisCallBack.sendDiscuss();
                }
            }
        });

        setContentView(rootView);

        this.setOutsideTouchable(true);

        this.setFocusable(true);

//        this.setAnimationStyle(R.style.MallPopupAnimation);

        setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        discussStr.setText("");
    }

    public void showPop() {
        showAtLocation(viewParent, Gravity.CENTER, 0, 0);
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }

    private SendDisCallBack sendDisCallBack;

    public SendDisCallBack getSendDisCallBack() {
        return sendDisCallBack;
    }

    public void setSendDisCallBack(SendDisCallBack sendDisCallBack) {
        this.sendDisCallBack = sendDisCallBack;
    }

    public interface SendDisCallBack {
        abstract void sendDiscuss();
    }

    public String getDiscuss() {
        return discussStr.getText().toString();
    }
}
